package com.gaoxk.interceptor;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.Phase;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * 入站日志拦截器：记录请求XML报文
 */
public class LoggingInterceptor extends AbstractSoapInterceptor {

    // 指定在"接收请求后"阶段执行
    public LoggingInterceptor() {
        super(Phase.RECEIVE);
    }

    @Override
    public void handleMessage(SoapMessage message) throws Fault {
        try {
            // 获取请求XML内容
            InputStream is = message.getContent(InputStream.class);
            byte[] buffer = new byte[is.available()];
            is.read(buffer);
            String requestXml = new String(buffer, "UTF-8");
            
            // 重置输入流（否则后续拦截器无法读取）
            message.setContent(InputStream.class, new ByteArrayInputStream(buffer));
            
            // 打印日志
            System.out.println("收到SOAP请求：\n" + requestXml);
        } catch (Exception e) {
            throw new Fault(new Exception("日志拦截器处理失败", e));
        }
    }
}
